﻿#region using
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AwesomeTanks.Sources.Media;
using AwesomeTanks.Sources.GameElements.ShootElements;
#endregion

namespace AwesomeTanks.Sources.GameElements.HUD
{

    /// <summary>
    /// Class of the component which draws all informations useful of the Player
    /// </summary>
    public class PlayerInfo
    {
        #region Fields
        public Player Player { get; set; }
        public Vector2 Position { get; set; }
        public Sprite TankLife { get; set; }
        public Sprite PlayerLife { get; set; }
        public Sprite Rocket { get; set; }
        public int? SpecialShotInitialWidth { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the component <see cref="PlayerInfo"/> class for the specified Player and at the specified Position.
        /// </summary>
        /// <param name="Player">The player.</param>
        /// <param name="position">The position.</param>
        public PlayerInfo(Player Player, Vector2 position)
        {
            this.Player = Player;
            this.Position = position;
            //Icons for Tank life and Player life in the Sprite Sheet
            TankLife = new Sprite(Art.SpriteSheet, Art.TankLife, new Vector2());
            PlayerLife = new Sprite(Art.SpriteSheet, Art.PlayerLife, new Vector2());
            Rocket = new Sprite(Art.SpriteSheet, Art.RocketShootSourceRectangle, new Vector2());
            Rocket.Color = Player.PlayerColor;
            TankLife.Color = Player.PlayerColor;
            PlayerLife.Color = Player.PlayerColor;
            SpecialShotInitialWidth = null;
        }
        #endregion

        #region Update and Draw

        /// <summary>
        /// Draws the component PlayerInfo with the specified sprite batch and the specified sprite font.
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="spritefont">The spritefont.</param>
        public void Draw(SpriteBatch spriteBatch,SpriteFont spritefont)
        {
            //Some magic numbers
            Vector2 positionPlayerName = new Vector2(Position.X + 5, Position.Y + 5);
            TankLife.Position = new Vector2(Position.X + 5, Position.Y + Art.TankLife.Height - 5);
            Vector2 positionTankLifeString = new Vector2(Position.X + Art.TankLife.Width , Position.Y + Art.TankLife.Height);

            PlayerLife.Position = new Vector2(Position.X + Art.PlayerLife.Width + 60, Position.Y + Art.PlayerLife.Height -5);
            Vector2 positionPlayerLifeString = new Vector2(Position.X + Art.PlayerLife.Width + Art.TankLife.Width + 50, Position.Y + Art.PlayerLife.Height);

            Rocket.Position = new Vector2(Position.X, Position.Y + Art.PlayerLife.Height + Art.RocketShootSourceRectangle.Height + 10);

            spriteBatch.DrawString(spritefont, Player.PlayerName + "   " + Player.Score, positionPlayerName, Player.PlayerColor);
            PlayerLife.Draw(spriteBatch);
            spriteBatch.DrawString(spritefont," x"+ Player.Life, positionTankLifeString, Player.PlayerColor);
            TankLife.Draw(spriteBatch);
            int LifeRemaining = (Player.Tank != null) ? Player.Tank.GetPercentageLifeRemaining() : 0;
            spriteBatch.DrawString(spritefont, " " + LifeRemaining + "%", positionPlayerLifeString, Player.PlayerColor);

            // Rocket loading indicator
            if (Player.Tank != null)
            {
                Weapon specialWeapon = Player.Tank.SecondaryWeapon;
                if (SpecialShotInitialWidth == null)
                {
                    SpecialShotInitialWidth = Rocket.SourceRectangle.Value.Width;
                }

                double specialWeaponLoadingRatio = specialWeapon.CurrentReloadTimer / specialWeapon.ReloadTimer;
                int specialWeaponWidthToDraw = (int)(specialWeaponLoadingRatio * SpecialShotInitialWidth);
                Rocket.SourceRectangle = new Rectangle(Rocket.SourceRectangle.Value.X, Rocket.SourceRectangle.Value.Y, specialWeaponWidthToDraw, Rocket.SourceRectangle.Value.Height);
                Rocket.Draw(spriteBatch);
            }
        }
        #endregion
    }
}
